﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class MVTVKPAY : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        MVTVKPAYData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;
        private string fromdate = "", enddate = "";   
        #endregion
        public MVTVKPAY()
        {
            InitializeComponent();
            this.Name = "订单付款情况";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new MVTVKPAYData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("PI", typeof(string));
            tableUI.Columns.Add("KUNDNR", typeof(string));
            tableUI.Columns.Add("PO", typeof(string));
            tableUI.Columns.Add("FROMDATE", typeof(string));
            tableUI.Columns.Add("ENDDATE", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding();
                txtPI.Focus();
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet setResult = new DataSet();            
                setUI.Clear();
                setUI.AcceptChanges();       
                string PO = txtPO.Text.Trim();
                string KUNDNR = txtKUNDNR.Text.Trim(); 
                //string STATUS = cbSTATUS.IsChecked.Value.ToString();
                string PI = txtPI.Text.Trim();
                
                if (PO == "")
                {
                    PO = "AAA";
                }
                //if (STATUS == "True")
                //{
                //    dwUI["STATUS"] = "J";
                //}
                //else
                //{
                //    dwUI["STATUS"] = "AAA";
                //}
                if (KUNDNR == "")
                {
                    KUNDNR = "AAA";
                }                
                if (PI == "")
                {
                    PI = "AAA";
                }
                if(fromdate == "")
                {
                    fromdate = "20000000";
                }
                if (enddate == "")
                {
                    enddate = "99999999";
                }

                dwUI["PO"] = PO;
                dwUI["PI"] = PI;
                dwUI["KUNDNR"] = KUNDNR;
                dwUI["FROMDATE"] = fromdate;
                dwUI["ENDDATE"] = enddate;
                setResult = m_Data.BuildMVTVKPAYData(dwUI);
                setUI.Merge(setResult);
                MasterDg.DataContext = setUI.Tables["VKPAY"];
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }       
        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new MVTVKPAYData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            MVTVKPAYReport m_rpt = new MVTVKPAYReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("OpenExcel");
        }
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }

        private void btnFINISH_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet Result;
                string orderno = tbORDERNO.Text;
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "select nvl(sum(trunc(b.vkpzuget*0.01)),0) as alqty, "
                    + " SUM((SELECT NVL(SUM(trunc(A1.VRPMENGE*0.01)),0) FROM VRPOS A1,VRKOPF B1 "
                    + " WHERE A1.VRPMANDANT = B1.VRKMANDANT AND A1.VRPRENR = B1.VRKRENR  "
                    + " AND B1.VRKFIBUKZ <> 'S' AND B1.VRKREART = 'RE' AND B1.VRKKOMPL = 'J' "
                    + " AND A1.VRPMANDANT = B.VKPMANDANT AND A1.VRPAUFKRS = B.VKPAUFKRS "
                    + " AND A1.VRPAUFLFD = B.VKPAUFLFD AND A1.VRPAUFPOS = B.VKPAUFPOS )) as vrqty "
                    + " from vkkopf a,vkpos b "
                    + " where a.vkkmandant = b.vkpmandant and a.vkksa = b.vkpsa "
                    + " and a.vkkaufkrs = b.vkpaufkrs and a.vkkauflfd = b.vkpauflfd "
                    + " and a.vkksa <>'AJ' "
                    + " and trim(a.vkkaufkrs)||'-'||to_number(a.vkkauflfd) = '"+orderno+"'"
                    ;

               Result = m_Data.Connect2Proxy(or1);
                if (Result.Tables.Count > 0)
                {
                    string ALQTY = "";
                    string VRQTY = "";
                    string SS = "";
                    foreach (DataRow dw in Result.Tables[0].Rows)
                    {
                        ALQTY = dw["alqty"].ToString().Trim();
                        VRQTY = dw["vrqty"].ToString().Trim();
                        if (ALQTY != VRQTY)
                        {
                            MessageBox.Show("请先将订单相应发票支付完成，才可设定完成！");
                            return;
                        }
                        else
                        {
                            OracleCommand or2;
                            or2 = OracleDB.CreateCommand();
                            or2.CommandText = "UPDATE VKPOS A SET A.VKPEAKZ = 'J',A.VKPERL='J' "
                                + " WHERE A.VKPSA <> 'AJ' "
                                + " AND trim(A.VKPAUFKRS)||'-'||to_number(A.VKPAUFLFD) = '" + orderno + "'"
                                ;

                            m_Data.Connect2Proxy(or2);
                        }
                    }
                } 
            }                
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            btnQuery_Click(null, null);
        }

        private void btnUNDO_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string orderno = tbORDERNO.Text;
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "UPDATE VKPOS A SET A.VKPEAKZ = ' ',A.VKPERL = ' ' "
                    + " WHERE A.VKPSA <> 'AJ' "
                    + " AND trim(A.VKPAUFKRS)||'-'||to_number(A.VKPAUFLFD) = '" + orderno + "'"
                    ;

                m_Data.Connect2Proxy(or1);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
            btnQuery_Click(null, null);
        }
                
        private void MasterDg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;
                DataRowView drv = e.AddedItems[0] as DataRowView;
                this.tbORDERNO.Text = drv.Row["CO"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        
    }
}
